﻿import numpy as np
# import importlib
# import utils
# importlib.reload(utils)

class medo_interface:
    def __init__(self,Environment):
        import environment_pool as env
        env.B_pool = Environment["B"]
        env.pS_pool = Environment["pS"]
        env.R1_pool = Environment["R1"]
        env.g_pool = Environment["g"]
        env.L2_pool = Environment["L2"]
        env.popsize_pool = Environment["popsize"]
        env.npar_pool = Environment["npar"]
        env.param_maxit_pool = Environment["param_maxit"]
        env.deltax_pool = Environment["deltax"]
        env.target_couple_pool = Environment["target_couple"]
        self.npar = env.npar_pool
    # 设置参数的上下限范围【lb,ub】，每个参数长度都是npar
    def set_boundary(self,lb,ub):
        import environment_pool as env
        env.lb_pool = lb
        env.ub_pool = ub
    # 设置爬行波数据
    def build_creeping_data(self,creeping_data):
        import environment_pool as env
        env.Tx_pool = creeping_data["Tx"]
        env.Rx_pool = creeping_data["Rx"]
        
        env.mesh_file_pool = creeping_data["mesh_file"]
        env.results_filename_pool = creeping_data["results_filename"]
        env.freq_pool = creeping_data["freq"]
        return env.build_geodesic_data()
    def build_benchmark(self,path,creeping_data):
        import environment_pool as env
        from utils import Geodesic_cfg_and_run,tri6_interp
        creeping_type = creeping_data['type']
        if(creeping_type==1):
            tri6_pts = creeping_data['Tx']  #发射机区域描述
            Rx = creeping_data['Rx']  #接收机安装位置列表
            N = np.size(Rx[:,0])  #接收机安装个数
        else:
            tri6_pts = creeping_data['Rx']  #接收机区域描述
            Tx = creeping_data['Tx']  #发射机安装位置列表
            N = np.size(Tx[:,0])  #发射机安装个数
        data = np.zeros((N),dtype=np.float32)
        def benchmark(x,npar):
            u = x[0];v= x[1]
            # if(u+v>1):
            #     return 200
            pts0 = tri6_interp(tri6_pts,u,v)
            Geodesic_cfg_data = self.build_creeping_data(creeping_data)
            #遍历多个接收机
            match_request = True
            for iN in np.arange(N):
                if(creeping_type==1):
                    Geodesic_cfg_data["<Rx_x>"] = Rx[iN,0]
                    Geodesic_cfg_data["<Rx_y>"] = Rx[iN,1]
                    Geodesic_cfg_data["<Rx_z>"] = Rx[iN,2]
                else:
                    Geodesic_cfg_data["<Tx_x>"] = Tx[iN,0]
                    Geodesic_cfg_data["<Tx_y>"] = Tx[iN,1]
                    Geodesic_cfg_data["<Tx_z>"] = Tx[iN,2]
                if not np.isnan(pts0[0]):
                    if(creeping_type==1):
                        Geodesic_cfg_data["<Tx_x>"] = pts0[0]
                        Geodesic_cfg_data["<Tx_y>"] = pts0[1]
                        Geodesic_cfg_data["<Tx_z>"] = pts0[2]
                    else:
                        Geodesic_cfg_data["<Rx_x>"] = pts0[0]
                        Geodesic_cfg_data["<Rx_y>"] = pts0[1]
                        Geodesic_cfg_data["<Rx_z>"] = pts0[2]
                        
                    results = -Geodesic_cfg_and_run(path,Geodesic_cfg_data)
                else:
                    results = 200
                if(-results<env.target_couple_pool[iN]):
                    match_request = False
                data[iN] = results
            # print(x,Tx_pts0,"%.30f"%data[0])
            return (np.min(data),match_request,data)
        env.benchmark_pool = benchmark
        # res = benchmark([0.53084008, 0.94519817],3)
        # res = benchmark([0.51, 0.32],3)
        # exit(-1)